package cn.ym.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.ym.domain.User;
import cn.ym.exception.BusinessException;
import cn.ym.mapper.UserMapper;
import cn.ym.query.UserQuery;
import cn.ym.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
/**
 * 用户的业务层
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void add(User user, HttpSession session){
        //空值检验
        if(StrUtil.isEmptyIfStr(user.getUserName())){
            throw new BusinessException("亲：账号不能为空");
        }
        if(StrUtil.isEmptyIfStr(user.getEmail())){
            throw new BusinessException("亲：邮箱不能为空");
        }
        if(StrUtil.isEmptyIfStr(user.getPhonenumber())){
            throw new BusinessException("亲：手机号不能为空");
        }
        if(StrUtil.isEmptyIfStr(user.getPassword())){
            throw new BusinessException("亲：密码不能为空");
        }

        User ses = (User)session.getAttribute("user_in_session");
        //设置用户名
        user.setRealName( ses.getRealName());
        user.setPassword(SecureUtil.md5(user.getPassword()));
        userMapper.add(user);
    }

    @Override
    public void update(User user,HttpSession session) {
        //空值检验
        if(StrUtil.isEmptyIfStr(user.getUserName())){
            throw new BusinessException("亲：账号不能为空");
        }
        if(StrUtil.isEmptyIfStr(user.getEmail())){
            throw new BusinessException("亲：邮箱不能为空");
        }
        if(StrUtil.isEmptyIfStr(user.getPhonenumber())){
            throw new BusinessException("亲：手机号不能为空");
        }
        if(StrUtil.isEmptyIfStr(user.getPassword())){
            throw new BusinessException("亲：密码不能为空");
        }


        User ses = (User)session.getAttribute("user_in_session");
        //设置用户名
        user.setRealName(ses.getRealName());
        userMapper.update(user);
    }

    @Override
    public void delete(Long id) {
        userMapper.delete(id);
    }

    @Override
    public User findOne(Long id) {
        return userMapper.findOne(id);
    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public List<User> queryPage(UserQuery userQuery) {
        //开启分页
        /**1.自动计算总数量
         * 2.根据驱动名计算 limit的第一个参数
         * 3.分页返回结果
         */
       PageHelper.startPage(userQuery.getCurrentPage(), userQuery.getPageSize());
       return userMapper.queryPage(userQuery);
    }

    @Override
    public void Patchdel(Long[] ids) {
        userMapper.Patchdel(ids);
    }






    @Override
    public User login(Map<String, String> map) {
        String account = map.get("account");
        String checkPass = map.get("checkPass");
        //一：校验 - 空值校验
        if(StrUtil.isEmptyIfStr(account)){
            throw new BusinessException("账号不能为空");
        }
        if(StrUtil.isEmptyIfStr(checkPass)){
            throw new BusinessException("密码不能为空");
        }
        //二：校验 - 用户名校验
        User dbUser = userMapper.findByUsername(account);
        if(dbUser == null){
            throw new BusinessException("用户名错误");
        }

        //三：校验 - 密码校验
        String md5Pwd = SecureUtil.md5(checkPass);
        if(!md5Pwd.equals(dbUser.getPassword())){
            throw new BusinessException("密码错误");
        }
        return dbUser;
    }




}